Model Steps

  • STEP

    A model is a class. It provides you with an easy way to update, insert or retrieve data from the tables.

    1 Working

    1. Each table must have a model.

    1. Table name and model name must be same, Eg: student

    2. DB tables should be in lower case, with underscores to separate words (snake_case), and should be in plural form. Eg: students, student_profiles, teachers, teacher_profiles

    3. A model should be in PascalCase/CapitalCase. They should be in singular, no spacing between words, and capitalised., Eg: Student, StudentProfile

    2. Fields in a table act as the Model variables.

    1. Table column names should be in snake_case (underscores between words) - in lower case Eg: name , email, date_of_birth

    2. Model variable name and table field name must be same. Eg: name , email, date_of_birth

    3. A table acts as a class , So We need object of the table (model) to set or get the field or class variable

    2 Create a model class

    1 Create the model in App\Models folder

    2 import the parent class Illuminate\Database\Eloquent\Model;

    3 extends from Model class

    Syntax

    
                                    class ClassName extends Model{
    
                                    }
                                    

    Structure or minimal code

    
                                            namespace App\Models;
                                            use Illuminate\Database\Eloquent\Model;
                                            class Student extends Model
                                            {
                                                    //
                                            }
                                    

    3 Insert or create new record in to a table

    save()

    1. Open your controller file

    2. Import the model class

    3. Create object of the model

    4. Assign the value to class variable (table fields)

    5. Use save() for saving new record into a table

    
                                    namespace App\Http\Controllers;
                                    
                                    use Illuminate\Http\Request;
                                    use App\Http\Controllers\Controller;
    
                                    //2. import model
                                    use App\Models\Student;
    
                                    class StudentController extends Controller
                                    {
    
    
                                            public function store(Request $request)
                                            {
                                                    //3. create object
                                                    $student = new Student;
    
                                                    //4. assign value
                                                    $student->name = "Manoj";
    
                                                    //5. save to table
                                                    $student->save();
    
                                                    
                                            }
    
    
                                    }
                                    

    4. Retrieval of Records

    1. get() : return array or collection of objects or records (no parameters pass to the function)

    2. first() : return single object or record. (No parameters pass to the function)

    3. find($id) : return single object or record. Note that there is parameter

    
                                    $student = Student::get();
    
                                    //OR
                                    $student = Student::first();
    
                                    //OR
                                    $student = Student::find(1);
                                     

    5. Update Records

    find() and save()

    1. Find and retrieve the record using find()

    2. Set attributes value

    3. Call save() method

    
                                     //1. find record       
                                     $student=Student::find(1);
    
                                     //2. assign value
                                     $student->name = "vijayan";
    
                                     //3. update record
                                     $student->save()
    
                                     

    6. Delete Records

    find() and delete()

    1. Retrieve record to be deleted using find()

    2. Call delete() method

    
                                     //1. find record       
                                     $student = Student::find(2);
    
                                     //2. delete from db
                                     $student ->delete();